import axios from 'axios'
import { Indicator } from 'mint-ui';
import store from '@/store'
import router from '@/router'
export default{
	//公共默认方式
	common:{
		method:"GET",
		data:{},//post
		params:{},//get
        headers:{}
	},
	$axios( options={} ){
		
		options.method = options.method || this.common.method;
		options.data = options.data || this.common.data;
		options.params = options.params || this.common.params;
        options.headers = options.headers || this.common.headers;
        
		//请求之前
		Indicator.open('加载中...');
        
        //有没有传入token
        if(   options.headers.token    ){
            //判断有没有token的值 ==> 有没有登录
            if( !store.state.user.token ){
               return router.push('/login');
            }
            options.headers.token = store.state.user.token;
        }
        
		return axios(options).then(v=>{
            
            
            
			let data = v.data.data;
            
            if( data.code == 1000){
                Indicator.close();
                return router.push('/login');
            }
            
			return new Promise((res,rej)=>{
				if( !v ){
					return rej();
				}
				//结束
				setTimeout(()=>{
					Indicator.close();
				},400)
				res( data );
			})
			
		})
	}
}